/**
 * 获取产品数据
 */
define(function (require, exports, module) {
    
    var moment = require("moment");
    module.exports = {
		//查询各时期房价
    	roomPriceData: function (widget,ret) {
    		var roomTypeMap = {};
    		for(var i =0;i<ret.length;i++){
    			var roomType = ret[i].roomType.name;
    			if(!roomTypeMap[roomType]){
    				roomTypeMap[roomType] = {
						standardPrice: {},
						vipPrice: {},
						protocolPrice:{},
						roomType:ret[i].roomType
    				};
    			}
    			if(!roomTypeMap[roomType].standardPrice[ret[i].standardPrice]){
    				roomTypeMap[roomType].standardPrice[ret[i].standardPrice] = {
    					price: ret[i].standardPrice,
    					type: "standardPrice",
    					saleDate: []
    				};
    			}
    			roomTypeMap[roomType].standardPrice[ret[i].standardPrice].saleDate.push(ret[i].saleDate);
    			
    			if(!roomTypeMap[roomType].vipPrice[ret[i].vipPrice]){
    				roomTypeMap[roomType].vipPrice[ret[i].vipPrice] = {
    					price: ret[i].vipPrice,
    					type: "vipPrice",
    					saleDate: []
    				};
    			}
    			roomTypeMap[roomType].vipPrice[ret[i].vipPrice].saleDate.push(ret[i].saleDate);
    			
    			if(!roomTypeMap[roomType].protocolPrice[ret[i].protocolPrice]){
    				roomTypeMap[roomType].protocolPrice[ret[i].protocolPrice] = {
    					price: ret[i].protocolPrice,
    					type: "protocolPrice",
    					saleDate: []
    				};
    			}
    			roomTypeMap[roomType].protocolPrice[ret[i].protocolPrice].saleDate.push(ret[i].saleDate);
    		}
    		
    		//循环房型
    		for(var j in roomTypeMap){
    			var  prices = [];
    			var protocolPriceItems = roomTypeMap[j].protocolPrice;
    			var vipPriceItems = roomTypeMap[j].vipPrice;
    			var standardPriceItems = roomTypeMap[j].standardPrice;
    			for(var p in protocolPriceItems){
    				prices.push(protocolPriceItems[p]);
    			}
    			for(var v in vipPriceItems){
    				prices.push(vipPriceItems[v]);
    			}
    			for(var s in standardPriceItems){
    				prices.push(standardPriceItems[s]);
    			}
    			roomTypeMap[j].items = prices;
    		}
    		
    		widget.set("roomTypeMap",roomTypeMap);
    		return roomTypeMap;
        },
        //获得房价、房价对应的出售日期
        getRoomPriceItem:function(widget,priceItems){
        	var ret = [];
        	var detailClick = widget.detailClick;
        	var typeNum = {
        			vipPrice:0,
        			standardPrice:0,
        			protocolPrice:0
        		};
        	//判断是否是体验客户
        	var nonMemberGroup = widget.get("tempGridRowData").nonMemGroupReserve.nonMemberGroup,
	    		signUpType;
	    	if(nonMemberGroup){
	    		signUpType = nonMemberGroup.signUpType;
	    	}
	    	
        	for(var i =0;i<priceItems.length;i++){
        		var cItemsPrice = priceItems[i],
        			csaleDate = cItemsPrice.saleDate;
            	if(cItemsPrice.checkInPrice){
            		checkInPrice = cItemsPrice.checkInPrice;
            	}else{
            		checkInPrice = "";
            	}
            	
        		ret.push({
					name: "roomPrice_" + cItemsPrice.type + "_" + cItemsPrice.price,
					label: "房价" + (typeNum[cItemsPrice.type]+1),
					readonly: true,
					defaultValue:cItemsPrice.price
				});
        		ret.push({
        			name:"livePrice_" + cItemsPrice.type + "_" + cItemsPrice.price,
					label:"入住价" + (typeNum[cItemsPrice.type]+1),
					validate: ["required"],
					readonly:detailClick,
					/*format:function(value){
						var param = widget.get("travelContract");
						if(param.unit.key=="Day"){
							value = param.price/param.balTime;
						}
						return value;
					},*/
					/*format:function(val,rowData){
						if(rowData.signUpRegister.travelContract.unit.key=="Day"){
							val = rowData.signUpRegister.travelContract.price/rowData.signUpRegister.travelContract.balTime;
						}
						return Number(val).toFixed(2);
					},
					onBeforeEdit:function(rowData){
						if(rowData.signUpRegister.travelContract.unit.key=="Day"){
							return false;
						}
					},*/
        		
					
					defaultValue:checkInPrice?Number(checkInPrice).toFixed(2):"",
						
					exValidate: function(value) {
                        if(!isNaN(value) && parseInt(value) >= 0){
                        	return true;
                   		 } else{
                   			return "房价必须输入数字且不能为负！";
                   		 }
                    }
				});
        		//判断是否是体验客户
        		if(signUpType && signUpType.key == "Experience"){
        			var threeMealsItem = {
            				name:"threeMeals_" + cItemsPrice.type + "_" + cItemsPrice.price,
        					type: "checkbox",
        					label:"",
        					list: [{
        		                key: "true",
        		                value: "是否含三餐"
        		            }]
            			}
        			
        			if(cItemsPrice.threeMeals){
        				threeMealsItem.defaultValue = "true";
        			}
        			ret.push(threeMealsItem);
        		}else{
        			var breakfastItems = {
        					name:"breakfast_" + cItemsPrice.type + "_" + cItemsPrice.price,
        					type: "checkbox",
        					label:"",
        					list: [{
        		                key: "true",
        		                value: "是否含早餐"
        		            }]
        				}
        			if(cItemsPrice.breakfast){
        				breakfastItems.defaultValue = "true";
        			}
        			ret.push(breakfastItems);
        		}
        		for(var j=0;j<csaleDate.length;j++){
        			var saleDateValue = {liveDate:csaleDate[j],roomPrice:cItemsPrice.price};
        			ret.push({
        				name:"saleDate_" + cItemsPrice.type + "_" + cItemsPrice.price + "_" +j,
        				type:"hidden",
        				defaultValue: JSON.stringify(saleDateValue)
        			})
        		}
        		typeNum[cItemsPrice.type]++;
        	}
			return ret;
        },
        //处理已保存过的房价
        processPriceData:function(roomType,newItems){
        	
        	var priceType = roomType.priceType.key,
        		pricesData = roomType.signUpRoomPrices;
        	
        	var mapPrice = {},
			    resultPrice = [];
			for(var i = 0; i < pricesData.length; i++){
			    var pricesItems = pricesData[i];
			    if(!mapPrice[pricesItems.roomPrice]){
			    	mapPrice[pricesItems.roomPrice] = {
			        	price: pricesItems.roomPrice,
			        	checkInPrice:pricesItems.checkInPrice,
			        	threeMeals:pricesItems.threeMeals,
			        	breakfast:pricesItems.breakfast,
			            saleDate: [],
			            type:priceType
			        };
			    }
			    mapPrice[pricesItems.roomPrice].saleDate.push(pricesItems.liveDate);
			}
			 for(var i in mapPrice){
				 resultPrice.push(mapPrice[i])
			 }
			if(resultPrice){
				for(var j=0;j<resultPrice.length;j++){
					resultPrice[j].saleDate.sort(function(v1,v2){
						if(v1>v2){
							return 1;
						}else{
							return -1;
						}
					});
				}
			}
			
			var newPrice = [];
        	for(var n=0;n<newItems.length;n++){
        		if(priceType == newItems[n].type){
        			newPrice.push(newItems[n]);
        		}
        	}
			
        	
        	
        	var newData = [];
    		$.each(resultPrice,function(j,rp){
    			$.each(newPrice,function(i,n){
    				if(rp.price == n.price){
    					$.merge( rp.saleDate, n.saleDate);
    					$.unique(rp.saleDate);
    					newData.push(i);
    				}
    			});
    		});
    		
    		for(var i = newData.length-1; i >= 0; i--){
    			newPrice.splice(newData[i],1);
    		}
    		
    		$.merge( resultPrice, newPrice);
			
			return resultPrice;
        },
        //实际房间数量
        getRealityNum:function(pkRoomType,data){
        	var roomArr = [];
        	for(var i=0;i<data.length;i++){
        		if(data[i].roomRegister.room &&( pkRoomType == data[i].roomRegister.room.roomType.pkRoomType)){
        			roomArr.push(data[i].roomRegister.room.pkRoom);
        		}
        	}
        	$.unique(roomArr);
        	return roomArr.length;
        },
        //获得房型
        getFormItems:function(widget,cItems,nonMemberType,roomType){
        	var defaultItems = [],
        	realRoom = 0,
        	roomPersonalInfos = widget.get("tempGridRowData").roomPersonalInfos;
        	var pkRoom = this.getRealityNum(roomType.roomType.pkRoomType,roomPersonalInfos);
        	if(nonMemberType){
        		 defaultItems = ([{
    				name: "roomType",
    				label: "预定房型",
    				readonly: true,
    				defaultValue:roomType.roomType.name
    			},{
    				name: "roomNum",
    				label: "预定房间数量",
    				readonly: true,
    				defaultValue:roomType.count
    			},{
    				name: "reservePrice",
    				label: "预定房价",
    				readonly: true,
    				defaultValue:roomType.payables
    			},{
    				name:"pkRoomType",
    				type:"hidden",
    				defaultValue:roomType.roomType.pkRoomType
    			}]);
        	}else{
        		 defaultItems = ([{
    				name: "roomType",
    				label: "房型",
    				readonly: true,
    				defaultValue:roomType.roomType.name
    			},{
    				name:"pkRoomType",
    				type:"hidden",
    				defaultValue:roomType.roomType.pkRoomType
    			}])
        	}
        	var selectPrice = {
    				name: "selectPrice",
    				label: "选择参考价",
    				type:"select",
    				validate: ["required"],
    				options: [{
    	                key: "standardPrice",
    	                value: "标准价"
    	            }, {
    	                key: "vipPrice",
    	                value: "会员价"
    	            }, {
    	                key: "protocolPrice",
    	                value: "协议价"
    	            }],
    				events: {
                        "change": function(e) {
                        	var roomTypeFormData = widget.get("roomTypeFormData"),
                        		selectPrice = e.target.value;
                        	for(var i=0;i<roomTypeFormData.length;i++){
                        		
                        		var roomForm = roomTypeFormData[i],
                        			roomFormId = roomForm.get("model").id,
                        			cFormId = $(e.currentTarget).parents("form").attr("id"),
                        			items = roomForm.get("model").items;
                        		if(cFormId == roomFormId){
                        			for(var k=0;k<items.length;k++){
                            			if(items[k].name.indexOf("roomPrice_") >=0 || items[k].name.indexOf("livePrice_") >=0){
                            				roomForm.$("div[el-data-component="+items[k].name+"]").parent().removeClass("hidden");
                            			}
                            		}
                            		for(var k=0;k<items.length;k++){
                            			if(items[k].name.indexOf(selectPrice) == -1 && items[k].name.indexOf("Price_") != -1){
                            				roomForm.$("div[el-data-component="+items[k].name+"]").parent().addClass("hidden");
                            			}
                            		}
                        		}                    		
                        	}
                        }
                    }
    			}
        	if(roomType.priceType){
        		selectPrice.defaultValue = roomType.priceType.key
        	}else{
        		selectPrice.defaultValue = "standardPrice"
        	}
        	if(widget.continueAdd){
        		selectPrice.readonly = true;
        	}
        	defaultItems.push(selectPrice);
        	
        	defaultItems.push({
				name: "realityNum",
				label: "实际房间数量",
				readonly: true,
				defaultValue: pkRoom
			},{
				name:"hiddentext",
				label: "",
				type:"text",
				"break": true
			},{
				name:"version",
				type: "hidden",
				defaultValue: roomType.version
			});
        	
        	//是否保存过入住价
        	var signUpRoomPrices = roomType.signUpRoomPrices;
        	var priceItems = cItems;
        	if(signUpRoomPrices && signUpRoomPrices.length>0){
        		priceItems = this.processPriceData(roomType,cItems);
        	}      	
        	defaultItems = defaultItems.concat(this.getRoomPriceItem(widget,priceItems));
        	
        	var pkSignUpRoom = {
        			name:"pkSignUpRoom",
    				type:"hidden"
    			};			
			if(roomType.pkSignUpRoom){
				pkSignUpRoom.defaultValue = roomType.pkSignUpRoom;
			}
        	defaultItems.push(pkSignUpRoom);
			defaultItems.push({
				name:"checkInPriceMark",
				label:"入住价说明",
				type:"textarea",
				"break": true,
				defaultValue:roomType.checkInPriceMark
			});
			return defaultItems;
        },
        cloneObj:function(obj){
		    var str, newobj = obj.constructor === Array ? [] : {};
		    if(typeof obj !== 'object'){
		        return;
		    } else if(window.JSON){
		        str = JSON.stringify(obj), //系列化对象
		        newobj = JSON.parse(str); //还原
		    } else {
		        for(var i in obj){
		            newobj[i] = typeof obj[i] === 'object' ? 
		            cloneObj(obj[i]) : obj[i]; 
		        }
		    }
		    return newobj;
		}
    };
});
